package com.md.test;

import com.md.entity.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.beans.Transient;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class student {
    public SqlSession getsqlSession() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        return sqlSession;
    }
    @Test
    public void selectAllStudent() throws IOException {
        SqlSession sqlSession = getsqlSession();
        List<Student> AllStudent = sqlSession.selectList("selectAllStudent");
        System.out.println(AllStudent);
    }
    @Test
    public void selectStudentById() throws IOException {
        SqlSession sqlSession = getsqlSession();
        Student student = sqlSession.selectOne("selectStudent", 1);
        System.out.println(student);
    }
    @Test
    public void insertStudent() throws IOException {
        SqlSession sqlSession = getsqlSession();
        Student student = new Student(0, "王五");
        sqlSession.insert("insertStudent",student);
        sqlSession.commit();
    }
    @Test
    public void deleteStudentBYId() throws IOException {
        SqlSession sqlSession = getsqlSession();
        sqlSession.delete("deleteStudent",1);
        sqlSession.commit();
    }
    @Test
    public void updateStudentById() throws IOException {
        SqlSession sqlSession = getsqlSession();
        Student student = new Student(2,"赵六");
        sqlSession.update("updateStudent",student);
        sqlSession.commit();
    }
}
